package cn.yinyu.queue.module.ecg.controller.admin.jobrecord;

import cn.yinyu.queue.framework.security.core.util.SecurityFrameworkUtils;
import cn.yinyu.queue.module.ecg.dal.dataobject.jobrecord.JobRecordStatisticDO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.yinyu.queue.framework.common.pojo.PageParam;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.pojo.CommonResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.success;

import cn.yinyu.queue.framework.excel.core.util.ExcelUtils;

import cn.yinyu.queue.framework.apilog.core.annotation.ApiAccessLog;
import static cn.yinyu.queue.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.yinyu.queue.module.ecg.controller.admin.jobrecord.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.jobrecord.JobRecordDO;
import cn.yinyu.queue.module.ecg.service.jobrecord.JobRecordService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 工作记录")
@RestController
@RequestMapping("/ecg/job-record")
@Validated
public class JobRecordController {

    @Resource
    private JobRecordService jobRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建工作记录")
    @PreAuthorize("@ss.hasPermission('ecg:job-record:create')")
    public CommonResult<Long> createJobRecord(@Valid @RequestBody JobRecordSaveReqVO createReqVO) {
        return success(jobRecordService.createJobRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工作记录")
    @PreAuthorize("@ss.hasPermission('ecg:job-record:update')")
    public CommonResult<Boolean> updateJobRecord(@Valid @RequestBody JobRecordSaveReqVO updateReqVO) {
        jobRecordService.updateJobRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工作记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ecg:job-record:delete')")
    public CommonResult<Boolean> deleteJobRecord(@RequestParam("id") Long id) {
        jobRecordService.deleteJobRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工作记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:job-record:query')")
    public CommonResult<JobRecordRespVO> getJobRecord(@RequestParam("id") Long id) {
        JobRecordDO jobRecord = jobRecordService.getJobRecord(id);
        return success(BeanUtils.toBean(jobRecord, JobRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工作记录分页")
    @PreAuthorize("@ss.hasPermission('ecg:job-record:query')")
    public CommonResult<PageResult<JobRecordRespVO>> getJobRecordPage(@Valid JobRecordPageReqVO pageReqVO) {
        PageResult<JobRecordDO> pageResult = jobRecordService.getJobRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JobRecordRespVO.class));
    }

    @GetMapping("/doctor-page")
    @Operation(summary = "获得用户自己的工作记录分页")
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    public CommonResult<PageResult<JobRecordRespVO>> getJobRecordDoctorPage(@Valid JobRecordPageReqVO pageReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();

        pageReqVO.setDocId( userId );
        PageResult<JobRecordDO> pageResult = jobRecordService.getJobRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JobRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工作记录 Excel")
    @PreAuthorize("@ss.hasPermission('ecg:job-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobRecordExcel(@Valid JobRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JobRecordDO> list = jobRecordService.getJobRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工作记录.xls", "数据", JobRecordRespVO.class,
                        BeanUtils.toBean(list, JobRecordRespVO.class));
    }

    @GetMapping("/job-statistic")
    @Operation(summary = "工作量统计")
    @PreAuthorize("@ss.hasPermission('ecg:job-record:statistic')")
    public CommonResult<List<JobRecordStatisticVO>> getJobStatistic(@Valid JobRecordPageReqVO pageReqVO) {
        List<JobRecordStatisticDO> statisticDOList = jobRecordService.getJobStatistic(pageReqVO.getJobTime()[0], pageReqVO.getJobTime()[1]);
        return success(BeanUtils.toBean(statisticDOList, JobRecordStatisticVO.class));
    }
}

